﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Happy.ExtentionMethods
{
    /// <summary>
    /// 针对参数或变量的校验器，如果验证失败就抛出<see cref="ApplicationException"/>异常。
    /// </summary>
    /// <remarks>
    /// 为了方便调用，该类的方法除了"Require"之外都是扩展方法。
    /// </remarks>
    public static partial class Check
    {
        /// <summary>
        /// <paramref name="directory"/>对应的路径必须存在，且<paramref name="directory"/>不能是空引用。
        /// </summary>
        public static void TheCorrespondingDirectoryMustExist(this string directory)
        {
            directory.MustNotNull("directory");

            Require(Directory.Exists(directory), string.Format(Resource.Messages.Error_TheCorrespondingDirectoryMustExist, directory));
        }

        /// <summary>
        /// 名称为<paramref name="variableName"/>的字符串的值（<paramref name="str"/>）不能是空引用且不能是空白字符串。
        /// </summary>
        public static void MustNotNullAndNotWhiteSpace(this string str, string variableName)
        {
            Require(!string.IsNullOrWhiteSpace(str), string.Format(Resource.Messages.Error_MustNotNullAndNotWhiteSpace, variableName));
        }

        /// <summary>
        /// 名称为<paramref name="variableName"/>的集合的值（<paramref name="collection"/>）的长度必须大于零，且<paramref name="collection"/>不能是空引用。
        /// </summary>
        public static void MustNotEmpty<T>(this IEnumerable<T> collection, string variableName)
        {
            collection.MustNotNull(variableName);

            Require(collection.Any(), string.Format(Resource.Messages.Error_MustNotEmpty, variableName));
        }

        /// <summary>
        /// 名称为<paramref name="variableName"/>的参数或变量的值（<paramref name="obj"/>）不能是空引用。
        /// </summary>
        public static void MustNotNull(this object obj, string variableName)
        {
            Require(obj != null, string.Format(Resource.Messages.Error_MustNotNull, variableName));
        }

        /// <summary>
        /// <paramref name="assertion"/>必须为true。
        /// </summary>
        public static void Require(bool assertion, string message)
        {
            if (assertion)
            {
                return;
            }

            throw new ApplicationException(message);
        }

        /// <summary>
        /// 名称为<paramref name="variableName"/>的参数或变量的值（<paramref name="value"/>）必须在某个范围。
        /// </summary>
        public static void MustBetween<T>(this T value, string variableName, T start, T end)
            where T : IComparable<T>
        {
            Require(value.CompareTo(start) >= 0 && value.CompareTo(end) <= 0, string.Format(Resource.Messages.Error_MustBetween, variableName, start, end));
        }

        /// <summary>
        /// 名称为<paramref name="variableName"/>的参数或变量的值（<paramref name="value"/>）必须大于指定的值。
        /// </summary>
        public static void MustGreaterThan<T>(this T value, string variableName, T target)
            where T : IComparable<T>
        {
            Require(value.CompareTo(target) > 0, string.Format(Resource.Messages.Error_MustGreaterThan, variableName, target));
        }

        /// <summary>
        /// 名称为<paramref name="variableName"/>的参数或变量的值（<paramref name="value"/>）必须大于等于指定的值。
        /// </summary>
        public static void MustGreaterThanEqual<T>(this T value, string variableName, T target)
            where T : IComparable<T>
        {
            Require(value.CompareTo(target) >= 0, string.Format(Resource.Messages.Error_MustGreaterThanEqual, variableName, target));
        }
    }
}
